//
//  SSEmoticonManager.swift
//  weibo
//
//  Created by 邵帅 on 16/7/2.
//  Copyright © 2016年 groupNineObject. All rights reserved.
//

import UIKit

class SSEmoticonManager: NSObject {

    // 定义数组接收数据
    static var recentEmoticon: [SSEmoticon] = [SSEmoticon]()
    
    static var defaultEmoticon: [SSEmoticon] = SSEmoticonManager.readEmoticons("com.sina.default")
    
    static var emojiEmoticon: [SSEmoticon] = SSEmoticonManager.readEmoticons("com.apple.emoji")
    
    static var lxhEmoticon: [SSEmoticon] = SSEmoticonManager.readEmoticons("com.sina.lxh")
    
    
    class func addRecentEmoticon(emoticon: SSEmoticon) {
        
        // 如何判断重复
        // 第一种方式: 遍历
        // 第二种: 数组有一个 contains 方法,能够判断重复
        if !recentEmoticon.contains(emoticon){
            
            // 把最新点击的按钮,放在第一个
            recentEmoticon.insert(emoticon, atIndex: 0)
            // 最近表情有 20个就够了
            if recentEmoticon.count >= 20 {
                // 删除最后一个
                recentEmoticon.removeLast()
            }
            //
            
        }
        
        // 最近表情的存储
        
    }
    
    
    // 数组，统一管理数据数组
    class func getAllEmoticons() -> [[[SSEmoticon]]]{
        
        return [pageEmoticons(recentEmoticon),pageEmoticons(defaultEmoticon),pageEmoticons(emojiEmoticon),pageEmoticons(lxhEmoticon)]
        
    }
    
    // 定义一个方法，从bundle中读取数据
    class func readEmoticons(filePaths: String) ->[SSEmoticon] {
        
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        
        let filePath = (path as NSString).stringByAppendingPathComponent(filePaths)
        
        let emoticonPath = (filePath as NSString).stringByAppendingPathComponent("info.plist")
        
        let dict = NSDictionary(contentsOfFile: emoticonPath)!
        
        let emoticonArr = dict["emoticons"] as! [[String: AnyObject]]
        
        var result = [SSEmoticon]()
        
        for dict in emoticonArr {
            
            let model = SSEmoticon(dict: dict)
            
            model.path = filePath
            result.append(model)
        }
        
        return result
    }
    
    // 定义一个方法，对数据数组进行切割,每页20个
    class func pageEmoticons(emoticons: [SSEmoticon]) -> [[SSEmoticon]] {
        
        let page = (emoticons.count - 1) / 20 + 1
        
        var result = [[SSEmoticon]]()
        
        for i in 0..<page {
            
            let loc = i * 20
            var len = 20
            
            if loc + len > emoticons.count {
                
                len = emoticons.count - loc
            }
            
            let subArr = (emoticons as NSArray).subarrayWithRange(NSMakeRange(loc, len)) as? [SSEmoticon]
            
            result.append(subArr!)
            
        }
      
        return result
    }
    
    // 根据传入字符串，返回一个表情对象
    class func returnEmoticon(text: String?)->(SSEmoticon?){
        
        guard let text = text else {
            
            return nil
        }
        
        for emoticon in defaultEmoticon {
            
            if emoticon.chs == text {
                
                return emoticon
            }
        }
        
        for emoji in emojiEmoticon {
            
            if emoji.chs == text {
                
                return emoji
            }
        }
        
        return nil
    }
    
}
